<template>
  <div class="home">
    <div class="flex-box">
      <div class="flex-left">
        <div
          class="left-box-top"
          :class="{ 'active-color': currentIndex === -1 }"
          @click="handleDept('', -1)"
        >
          <div>
            <i class="el-icon-caret-bottom" />
            <i class="el-icon-folder-opened margin-right-m" /> 全部部门
          </div>
        </div>
        <div style="padding-left: 35px">
          <div
            v-for="(item, index) in aDList"
            :key="index"
            :class="{ 'active-color': currentIndex === index }"
            @click="handleDept(item, index)"
          >
            <div class="dept-box">
              <div>
                <i class="el-icon-document margin-right-m" />
                {{ item.archDeptName }}
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="flex-right">
        <div class="search-box">
          <el-button
            v-if="authType === 'nomalAuth' || authType === 'root'"
            type="primary"
            size="small"
            @click="addCurArch()"
          >新增</el-button>
          <div style="margin-left: 10px">
            档案名称:<el-input
              v-model="searchForm.archTitle"
              clearable
              size="small"
              placeholder="请输入搜索的档案名称"
              style="width: 200px; margin: 0 10px"
            />
          </div>
          <div>
            档案类别:
            <el-select
              v-model="searchForm.archCateCode"
              placeholder=""
              style="margin-right: 10px; width: 100px"
              clearable
              size="small"
            >
              <el-option
                v-for="item in categoryOptions"
                :key="item.archCateCode"
                :label="item.archCateName"
                :value="item.archCateCode"
              />
            </el-select>
          </div>
          <div>
            归档状态:<el-select
              v-model="searchForm.archState"
              clearable
              size="small"
              placeholder="请选择归档状态"
              style="margin: 0 10px; width: 100px"
            >
              <el-option
                v-for="item in stateOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>

          <div class="search-more-box">
            <el-tooltip
              class="item"
              effect="dark"
              content="更多检索条件"
              placement="top"
            >
              <el-button
                :icon="moreSearch ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                size="small"
                :type="moreSearch ? 'primary' : ''"
                @click="moreSearch = !moreSearch"
              />
            </el-tooltip>
            <div v-show="moreSearch" class="search-content-box">
              <div>
                浏览次数:<el-input
                  v-model="searchForm.visitCount"
                  style="margin-right: 10px; width: 100px"
                  size="small"
                  clearable
                  placeholder="输入次数"
                />
              </div>
              <div style="margin-top: 20px">
                借阅次数:
                <el-input
                  v-model="searchForm.borrowCount"
                  placeholder=""
                  clearable
                  style="margin-top: 10px; width: 140px"
                  size="small"
                />
              </div>
            </div>
          </div>
          <el-button
            type="primary"
            size="small"
            style="margin-left: 10px"
            @click="getData()"
          >检索</el-button>
        </div>
        <div class="table">
          <el-table
            v-loading="isLoading"
            border
            :data="listData"
            style="margin-top: 10px"
          >
            <el-table-column
              align="center"
              prop="archMainCode"
              width="200"
              label="档案编号"
            />
            <el-table-column align="center" prop="archTitle" label="档案名称" />
            <el-table-column
              align="center"
              prop="archCateCode"
              label="档案类别"
            >
              <template slot-scope="scope">
                <span
                  v-for="(item, index) in categoryOptions"
                  v-show="scope.row.archCateCode === item.archCateCode"
                  :key="index"
                >{{ item.archCateName }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="archPeriod"
              label="保管期(月)"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.archPeriod === 0">永久</span>
                <span v-else>{{ scope.row.archPeriod }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column align="center" prop="archRequireOther" label="档案规范其他需求" width="160">
        </el-table-column> -->
            <el-table-column align="center" label="归档状态">
              <template slot-scope="scope">
                <span
                  v-if="scope.row.archState === 0"
                  class="warning"
                >预归档</span>
                <span
                  v-if="scope.row.archState === 1 || scope.row.archState === 2"
                  class="primary"
                >接收待审核</span>
                <span
                  v-if="scope.row.archState === 3"
                  class="danger"
                >驳回</span>
                <span
                  v-if="scope.row.archState === 5"
                  class="success"
                >归档接受</span>
                <span
                  v-if="scope.row.archState === 10"
                  class="info"
                >档案封存</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="档案访问许可">
              <template slot-scope="scope">
                <span v-if="scope.row.accessState === 0">公开</span>
                <span v-else>隐藏</span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="visitCount"
              label="浏览次数"
            />
            <el-table-column
              align="center"
              prop="borrowCount"
              label="借阅次数"
            />
            <!-- <el-table-column align="center" prop="notes" label="备注"> </el-table-column> -->
            <el-table-column align="left" label="操作" width="160">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="detail(scope.row)"
                >详情</el-button>
                <el-button
                  v-show="scope.row.archState == 0 || scope.row.archState == 1"
                  type="primary"
                  size="mini"
                  @click="edit(scope.row)"
                >编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            :current-page.sync="page.pageNum"
            :page-sizes="[10, 20, 50]"
            :page-size="page.pageSize"
            layout="sizes, total, prev, pager, next"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <el-dialog title="详情" :visible.sync="detailDialog" width="45%">
      <el-form :model="selectRow" label-position="right" label-width="140px">
        <el-form-item label="档案名称">
          <div class="item-div">{{ selectRow.archTitle }}</div>
        </el-form-item>
        <el-form-item label="档案编号">
          <div class="item-div">{{ selectRow.archMainCode }}</div>
        </el-form-item>
        <el-form-item label="热点标签">
          <el-tag v-if="selectRow.archDeptHotName" type="success">{{
            selectRow.archDeptHotName
          }}</el-tag>
          <span v-else>暂无热点标签</span>
        </el-form-item>
        <el-form-item label="档案保管期(月)">
          <div class="item-div">
            {{ selectRow.archPeriod === 0 ? "永久" : selectRow.archPeriod }}
          </div>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-if="selectRow.notes"
            v-model="selectRow.notes"
            class="input form-width"
            type="textarea"
            :rows="3"
          />
          <span v-else>暂无备注</span>
        </el-form-item>
        <el-form-item label="档案附件">
          <span
            v-for="item in fileList"
            :key="item.archFilesId"
            type="primary"
            style="display: block"
          >{{ item.fileName }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="detailDialog = false">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑" :visible.sync="editDialog" width="45%">
      <el-form :model="selectRow" label-position="right" label-width="140px">
        <el-form-item label="档案名称">
          <el-input
            v-model="selectRow.archTitle"
            placeholder=""
            class="form-width"
          />
        </el-form-item>
        <el-form-item label="档案编号">
          <div>{{ selectRow.archMainCode }}</div>
        </el-form-item>
        <el-form-item label="热点标签">
          <el-tag v-if="selectRow.archDeptHotName" type="success">{{
            selectRow.archDeptHotName
          }}</el-tag>
          <span v-else>暂无热点标签</span>
          <!-- <span >{{ selectRow.archDeptHotName }}</span> -->
        </el-form-item>
        <el-form-item label="档案保管期">
          <el-input-number
            v-model="selectRow.archPeriod"
            :step="12"
            style="width: 150px"
            oninput="this.value = this.value.replace(/[^0-9]/g, '');"
            placeholder="0为永久"
          />
          <span style="color: #56575b"> 月( 0为永久 )</span>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="selectRow.notes"
            class="input form-width"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item label="档案附件">
          <span
            v-for="item in fileList"
            :key="item.archFilesId"
            type="primary"
            style="display: block"
          >{{ item.fileName }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="editDialog = false">取消</el-button>
        <el-button type="primary" @click="editDialog = false">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="新增存量档案"
      :modal-append-to-body="false"
      :visible.sync="addDialog"
      width="60%"
    >
      <div>
        <current-archiving ref="currentRef" />
      </div>
      <div slot="footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit()">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  qryAllArchMainByType,
  updateCount,
  queryFilesById,
  updateArchMainInfo
} from '@/api/archManage'
import { getAllCategory } from '@/api/archCategory'
import { addArchApplication } from '@/api/archApplication'
import CurrentArchiving from './currentArchiving.vue'
import { getAllArchDept } from '@/api/archDept'

export default {
  components: { CurrentArchiving },
  data() {
    return {
      userInfo: {},
      authType: '',
      isLoading: false,
      searchForm: {
        archTitle: '',
        archState: '',
        archCateCode: '',
        archPeriod: -1,
        archDeptCode: '',
        username: '',
        staffCode: '',
        visitCount: '',
        borrowCount: '',
        submitYear: '',
        archDeptHotName: ''
      },
      addForm: {
        archDeptClassName: '',
        archMainCode: '',
        archTitle: '',
        delivUnit: '',
        archPeriod: '',
        archRequireOther: '',
        archState: '',
        auditors: '',
        accessState: '',
        notes: ''
      },
      listData: [],
      detailDialog: false,
      selectRow: {},
      fileList: [],
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 10
      },
      editDialog: false,
      categoryOptions: [],
      stateOptions: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 0,
          label: '预归档'
        },
        {
          value: 1,
          label: '接收待审核'
        },
        {
          value: 3,
          label: '驳回'
        },
        {
          value: 5,
          label: '归档接受'
        },
        {
          value: 10,
          label: '档案封存'
        }
      ],
      addDialog: false,
      moreSearch: false,
      aDList: [],
      currentIndex: -1,
      archDeptName: ''
    }
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.authType = sessionStorage.getItem('authType')
    this.getData()
    this.getAllCategory()
    this.getAllArchDept()
  },
  methods: {
    // 获取左边部门列表
    getAllArchDept() {
      getAllArchDept({
        pageNum: -1,
        pageSize: -1
      }).then((res) => {
        this.aDList = res.data.records
      })
    },
    // 点击左边部门
    handleDept(item, index) {
      this.archDeptName = item.archDeptName
      this.currentIndex = index
      this.searchForm.archDeptCode = item ? item.archDeptCode : ''
      this.getData()
    },
    // 获取列表数据
    getData() {
      this.isLoading = true
      const { pageNum, pageSize } = this.page
      const param = {
        pageNum: pageNum,
        pageSize: pageSize,
        accessState: 0,
        ...this.searchForm,
        visitCount: this.searchForm.visitCount ? this.searchForm.visitCount : 0,
        borrowCount: this.searchForm.borrowCount ? this.searchForm.borrowCount : 0
      }
      qryAllArchMainByType(param)
        .then((res) => {
          if (res.code === '0') {
            this.listData = res.data.records
            this.page.total = parseInt(res.data.total)
          }
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    // 获取档案分类列表
    getAllCategory() {
      getAllCategory({
        pageNum: -1,
        pageSize: -1
      }).then((res) => {
        if (res.code === '0') {
          const list = [
            {
              archCateCode: '',
              archCateName: '全部',
              archCateId: '',
              archRequireOther: '',
              createtime: '',
              archCateType: 0,
              notes: ''
            }
          ]
          this.categoryOptions = list.concat(res.data.records)
        }
      })
    },
    // 点击新增存量档案
    addCurArch() {
      this.addDialog = true
      this.$nextTick(() => {
        this.$refs.currentRef.saveOriginForm()
        if (this.searchForm.archDeptCode !== '') {
          this.$refs.currentRef.addForm.delivUnit = this.searchForm.archDeptCode
          this.$refs.currentRef.getAllDeptCategory({
            pageNum: -1,
            pageSize: -1,
            archDeptName: this.archDeptName
          })
          return
        }
        this.$refs.currentRef.addForm.delivUnit = ''
      })
      // this.$router.push('/archManage/currentArchiving')
    },
    // 点击详情
    detail(row) {
      this.isLoading = true
      // 更新浏览和借阅次数
      updateCount({ type: 1, archMainId: row.archMainId })
      this.selectRow = row
      queryFilesById({ archMainCode: row.archMainCode })
        .then((res) => {
          if (res.code === '0') {
            this.fileList = res.data

            // console.log(this.fileList);
          }
        })
        .finally(() => {
          this.detailDialog = true
          this.isLoading = false
        })
    },
    // 在归档没审核前能修改档案名称、保管期和备注
    edit(row) {
      this.editDialog = true
      this.selectRow = row
    },
    updateArchMainInfo() {
      const param = {
        archMainId: this.selectRow.archMainId,
        archTitle: this.selectRow.archTitle,
        archPeriod: this.selectRow.archPeriod,
        notes: this.selectRow.notes
      }
      updateArchMainInfo(param).then((res) => {
        this.getData()
        this.editDialog = false
      })
    },
    borrow(row) {
      this.$prompt('请输入借阅天数', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^\+?[1-9]\d*$/,
        inputErrorMessage: '请输入正整数'
      }).then(({ value }) => {
        addArchApplication({
          during: value,
          staffCode4App: this.userInfo.username
            ? this.userInfo.username
            : this.userInfo.staffCode,
          appState: 0,
          archMainId: row.archMainId,
          archMainCode: row.archMainCode
        }).then((res) => {
          if (res.code === '0') {
            this.$message({
              type: 'success',
              message: '已发起借阅申请，请联系负责人进行审核'
            })
          }
        })
      })
    },
    // 点击提交
    handleSubmit() {
      this.$refs.currentRef.submit()
      this.getData()
      this.addDialog = false
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getData()
    }
  }
}
</script>

<style scoped>
.home {
  margin: 20px;
}
.flex-box {
  display: flex;
  justify-content: space-between;
}
.flex-left {
  width: 10%;
}
.flex-right {
  width: 89%;
}
.search-box {
  color: #606266;
  font-size: 15px;
  display: flex;
  align-items: center;
  position: relative;
}

.search-input {
  width: 200px;
}

/* .table {
  height: calc(100vh - 180px);
} */

.pagination {
  margin-top: 10px;
  align-items: center;
}
.form-width {
  width: 80%;
}
.item-div {
  padding: 0px 15px;
  border: 1px solid #eee;
  border-radius: 4px;
  width: 80%;
}
.danger {
  color: #f56c6c;
}
.primary {
  color: #409eff;
}
.success {
  color: #67c23a;
}
.warning {
  color: #e6a23c;
}
.info {
  color: #909399;
}
div /deep/ .el-dialog {
  margin-top: 40px !important;
  border-radius: 8px;
}
div /deep/ .el-dialog__body {
  padding: 0 25px;
}

.search-content-box {
  position: absolute;
  right: 100px;
  margin-top: 5px;
  background: white;
  border: 1px solid #eee;
  border-radius: 5px;
  padding: 20px 10px 20px 10px;
  box-shadow: 4px 3px 5px rgb(145 145 145 / 60%);
  z-index: 99;
}

.left-box-top {
  width: 100%;
  padding: 10px 10px 10px 2px;
  cursor: pointer;
}
.active-color {
  color: #076eff;
  font-weight: bold;
}
.dept-box {
  padding: 5px 0;
  cursor: pointer;
}
</style>
